import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/xindong_beauty_singleton.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/beauty/xindong_beauty_utils_view.dart';
import 'package:xindong/routes/call/controller/xindong_call_controller.dart';
import 'package:xindong/routes/call/controller/xindong_call_rtc_controller.dart';
import 'package:xindong/widgets/dialog/xindong_base_sheet.dart';
import 'package:xindong/widgets/gift/xindong_songli_sheet.dart';
import 'package:xindong/widgets/xindong_horizontal_buttons.dart';

// 工具底栏
class XinDongCallFooterView extends StatelessWidget {
  final double itemHeight = 42.0;

  const XinDongCallFooterView({super.key});

  // 送礼
  void sendGift() {
    final user = XinDongCallController.to.targetUser;
    XinDongSongLiSheet.show(
      user: user,
      onSuccess: (model, count) {
        Navigator.pop(currentContext());
        // showToast("发送成功", milliseconds: 2000);
      },
    );
  }

  // 设置美颜
  Future<void> setBeauty() async {
    await showSheetDialog(
      barrierColor: Colors.transparent,
      backgroundColor: Colors.transparent,
      sheet: const XinDongBeautyToolsView(),
    );
    XinDongBeautyManager.instance.save();
  }

  // 点击
  void onTap(
      {required XinDongHorizontalButtonType type,
      required bool isMicMute,
      required bool isHandsFree,
      required bool isCameraOff}) async {
    debugPrint(type.toString());
    final rtcController = XinDongCallRTCController.to;
    switch (type) {
      // 麦克风
      case XinDongHorizontalButtonType.microphone_off:
      case XinDongHorizontalButtonType.microphone_on:
        rtcController.setMicMute(!isMicMute);
        break;
      // 免提
      case XinDongHorizontalButtonType.voice_on:
      case XinDongHorizontalButtonType.voice_off:
        rtcController.setHandsFree(!isHandsFree);
        break;
      // 礼物
      case XinDongHorizontalButtonType.gift:
        sendGift();
        break;
      // 切换前后摄像头
      case XinDongHorizontalButtonType.reverse_lens:
        rtcController.switchCamera();
        break;
      // 摄像头开关
      case XinDongHorizontalButtonType.camera_on:
      case XinDongHorizontalButtonType.camera_off:
        rtcController.setCameraOff(!isCameraOff);
        break;
      case XinDongHorizontalButtonType.beauty:
        setBeauty();
        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    // 点击事件不实现
    return GestureDetector(
      child: Container(
        height: itemHeight,
        width: double.infinity,
        alignment: Alignment.center,
        margin: const EdgeInsets.only(left: 16, top: 10, right: 16, bottom: 20),
        child: GetBuilder<XinDongCallRTCController>(
          builder: (controller) {
            bool isMicMute = controller.isMicMute;
            bool isHandsFree = controller.isHandsFree;
            bool isCameraOff = controller.isCameraOff;

            List<XinDongHorizontaButtonClass> buttonClasss = [];
            if (XinDongCallController.to.currentCallType == XinDongCallType.videoCall) {
              // 切换前后摄像头
              buttonClasss.add(XinDongHorizontaButtonClass(
                  type: XinDongHorizontalButtonType.reverse_lens, iconName: AssetsRes.CALL_CAMERA_REVERSE));
              // 关闭/开启摄像头
              buttonClasss.add(XinDongHorizontaButtonClass(
                type: isCameraOff ? XinDongHorizontalButtonType.camera_off : XinDongHorizontalButtonType.camera_on,
                iconName: isCameraOff ? AssetsRes.ICON_CAMERA_OFF : AssetsRes.ICON_CAMERA_ON,
              ));
              // 关闭/开启麦克风
              buttonClasss.add(XinDongHorizontaButtonClass(
                type:
                    isMicMute ? XinDongHorizontalButtonType.microphone_off : XinDongHorizontalButtonType.microphone_on,
                iconName: isMicMute ? AssetsRes.CALL_MIC_OFF : AssetsRes.CALL_MIC_ON,
              ));
              // 美颜
              buttonClasss.add(XinDongHorizontaButtonClass(
                  type: XinDongHorizontalButtonType.beauty, iconName: AssetsRes.ICON_BEAUTY));
            } else {
              // 关闭/开启麦克风
              buttonClasss.add(XinDongHorizontaButtonClass(
                type:
                    isMicMute ? XinDongHorizontalButtonType.microphone_off : XinDongHorizontalButtonType.microphone_on,
                iconName: isMicMute ? AssetsRes.CALL_MIC_OFF : AssetsRes.CALL_MIC_ON,
              ));
              // 关闭/开启扬声器
              buttonClasss.add(XinDongHorizontaButtonClass(
                type: isHandsFree ? XinDongHorizontalButtonType.voice_on : XinDongHorizontalButtonType.voice_off,
                iconName: isHandsFree ? AssetsRes.CALL_VOICE_ON : AssetsRes.CALL_VOICE_OFF,
              ));
            }
            // 礼物
            buttonClasss.add(XinDongHorizontaButtonClass(
              type: XinDongHorizontalButtonType.gift,
              iconName: AssetsRes.BUTTON_GIFT,
            ));
            return XinDongHorizontalButtons(
              dataList: buttonClasss,
              size: itemHeight,
              onTap: (type) => onTap(
                type: type,
                isMicMute: isMicMute,
                isHandsFree: isHandsFree,
                isCameraOff: isCameraOff,
              ),
            );
          },
        ),
      ),
    );
  }
}
